<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

/**
 * 订单回收站管理
 * @icon fa fa-user
 */
class Ordrecycle extends Backend
{
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = 'order.id,subject.title,user.nickname,code,total';
    /**
     * 
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        // 订单模型
        $this->SubjectOrderModel = model('Subject.Order');
    }

    // 查看
    public function index()
    {

       
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为主表中的id
            $this->changeWhereName('id', 'order.id');
            // 将筛选中的where字段联表查询都有的时候，导致约束条件不明确，给指定的字段加别名
            $this->changeWhereName('createtime', 'order.createtime');

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SubjectOrderModel
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->SubjectOrderModel
                ->onlyTrashed()
                ->with(['subject', 'business'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 删除单个和批量删除
    public function del($ids = null)
    {
        $id = empty($ids) ? [] : explode(',', $ids);

        // 判断是否有没有当前数据
        foreach ($id as $item) {
            // onlyTrashed()  仅查询软删除数据
            $res = $this->SubjectOrderModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找当前分类');
            }
        }
        
        $result = $this->SubjectOrderModel->destroy($id,true);

        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error($this->SubjectOrderModel->getError());
        }
    }

    // 还原
    public function reduction($ids = null)
    {
        $id = empty($ids) ? [] : explode(',', $ids);

        $data = [];
        // 判断是否有没有当前数据
        foreach ($id as $item) {
            $res = $this->SubjectOrderModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找当前分类');
            }

            $list = ['id'=>$item, 'deletetime' => null];
            array_push($data, $list);
        }

        // 批量还原
        $result = $this->SubjectOrderModel->isUpdate()->saveAll($data);


        // $wheres = [
        //  'id' => ['in', $id]
        // ];
        // $result = $this->SubjectOrderModel->onlyTrashed()->where($wheres)->update(['deletetime' => null]);

        if ($result) {
            $this->success('还原成功');
        } else {
            $this->error($this->SubjectOrderModel->getError());
        }
    }
}
